package com.wbc.smartHome.dao;

import com.wbc.smartHome.model.Commodity;
import com.wbc.smartHome.model.CommodityLabel;
import com.wbc.smartHome.model.CommodityType;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface CommodityDao {
    List<Commodity> getCommodityList(Commodity commodity);

    @Insert("insert into commodity(name,typeid,img,address,first_price,now_price,content,adminid,oper_time)values (#{name},#{typeId},#{img},#{address},#{firstPrice},#{nowPrice},#{content},#{adminId},now())")
    void setCommodity(String name, String img, Integer typeId, double nowPrice, double firstPrice, String address, String content, int adminId);

    @Select("SELECT id,name,typeid,img,address,first_price,now_price,content,oper_time  \n" +
            "FROM commodity \n" +
            "where (first_price - now_price) >=50\n" +
            "ORDER BY (first_price - now_price) desc;\n" +
            "\n")
    List<Commodity> getSpecialOffer();

    List<Commodity> getCommodityListByTypeId(Integer typeId);

    Commodity getCommodityById(Integer id);

    @Select("SELECT id,name,typeid,img,address,first_price,now_price,content,oper_time  \n" +
            "FROM commodity  \n" +
            "WHERE id = (  \n" +
            "    SELECT commodityid  \n" +
            "    FROM commoditylabel_commodity  \n" +
            "    WHERE labelid = #{labelId}  \n" +
            ")")
    List<Commodity> getCommodityListByLabelId(Integer labelId);

    List<Commodity> getRecommendCommodityList();

    @Delete("delete from commodity where id = #{id}")
    void delete(int id);

    @Select("select id,name from commodity_type")
    List<CommodityType> getCommodityType();

    @Select("select id,name from commodity_label")
    List<CommodityLabel> getCommodityLabel();

    @Insert("insert into commodity_clabel(commodityid,labelid,adminid,oper_time) values (#{commodityId},#{labelId},#{adminId},now())")
    void setCommodityLabel(Integer labelId, Integer commodityId, int adminId);

    @Select("select id from commodity where name = #{name}")
    int getCommodityIdByName(String name);

    @Delete("delete from commodity_clabel where commodityid = #{commodityId}")
    void deleteCLabelbyCommodityId(int commodityId);

    @Delete("delete from commodity_clabel where commodityid = #{commodityId}")
    void deleteByCommoidtyId(int commodityId);

    @Update("update commodity set name = #{name},typeid=#{typeId},img=#{img},address=#{address},now_price=#{nowPrice},first_price=#{firstPrice},content=#{content},adminid=#{adminId} where id = #{id}")
    void updataCommodity(String name, Integer typeId, String img, String address, double nowPrice, double firstPrice, String content, int adminId, Integer id);

    @Select("select id,img from commodity")
    List<Commodity> getCommodityPhoto();
}
